import { http } from "@/utils/http";

export enum MaterialState {
  All = -1,
  Submitted = 0,
  Uploaded = 1,
  AuditSuccess = 2,
  AuditFail = 9
}

// 视频素材列表请求参数接口
export interface VideoListParams {
  state?: number; // 状态
  page?: number; // 页码
  size?: number; // 每页数量
  videoMode?: string; // 视频模式
  name?: string; // 名称
  adAccountIdList?: number[]; // 广告账户ID
  director?: string; // 导演
  editor?: string; // 编辑
  audioLabel?: string; // 音频语言标签
  videoLabel?: string; // 视频语言标签
}

// 视频素材列表项接口
export interface MaterialItem {
  id: number;
  materialName: string;
  materialMode: number;
  ossPath: string;
  state: number;
  ksVideoId: number;
  duration: number;
  adAccountId: number;
  accountName: string;
  editor: number;
  editorName: string;
  director: number;
  directorName: string;
  footageLanguageTag: string;
  audioLanguageTag: string;
  videoUrl: string;
  coverUrl: string;
  md5: string;
  height: number;
  width: number;
  size: number;
  errorMsg: string;
  createTime: string;
}

// 枚举选项接口
interface EnumOption {
  key: string;
  value: string;
}

// 枚举数据响应接口
export interface MaterialSelectResponse {
  audioLabel: EnumOption[]; // 声音语言标签
  auditState: EnumOption[]; // 审核状态
  videoLabel: EnumOption[]; // 视频语言标签
}

// 视频素材列表响应接口
export interface VideoListResponse {
  list: MaterialItem[];
  total: number;
  page: number;
  size: number;
}

export interface UserInfo {
  accountId: number;
  nickName: string;
  email: string;
}

// 编导/剪辑列表响应接口
export interface UserListResponse {
  list: UserInfo[];
  total: number;
}

// 素材上传参数接口
export interface MaterialUploadParams {
  name: string; // 视频名称
  videoLanguage: string; // 视频语言标签
  audioLanguage: string; // 声音语言标签
  adAccountIdList: number[]; // 广告账户id
  director: string; // 编导
  editor: string; // 剪辑师
  videoMode: "1" | "2"; // 1 竖版 2 横版
  file: File; // 视频文件
}

// 编辑素材接口
export interface BatchUpdateMaterialsParams {
  ids: number[];
  director: string;
  editor: string;
}

/**
 * 获取视频素材列表
 * @param params 查询参数
 */
export const getVideoList = (params: VideoListParams) => {
  return http.post<VideoListResponse, typeof params>(
    "/api/v1/material/video/list",
    { data: params }
  );
};

/**
 * 编辑视频(暂无批量更新接口，这里先使用循环请求)
 * @param params 查询参数
 */
export const updateMaterials = async (params: BatchUpdateMaterialsParams) => {
  const { ids, director, editor } = params;
  const requests = ids.map(id => {
    let params = { videoId: id, director, editor };
    return http.post<unknown, typeof params>("/api/v1/material/video/update", {
      params
    });
  });
  return await Promise.all(requests);
};

/**
 * 获取素材枚举
 */
export const getMaterialSelect = () => {
  return http.request<MaterialSelectResponse>("get", "/api/v1/material/select");
};

/**
 * 获取编导列表
 */
export const getDirectorList = () => {
  return http.post<UserListResponse, unknown>(
    "/api/v1/material/video/director_list"
  );
};

/**
 * 获取剪辑列表
 */
export const getEditorList = () => {
  return http.post<UserListResponse, unknown>(
    "/api/v1/material/video/editor_list"
  );
};

/**
 * 上传素材
 * @param params 上传参数
 */

export const uploadMaterial = (params: MaterialUploadParams) => {
  return http.post("/api/v1/material/video/upload", { data: params });
};
/**
 * 上传素材
 * @param params 上传参数
 */
// export const uploadMaterial = (params: MaterialUploadParams) => {
//   const formData = new FormData();
//   Object.keys(params).forEach(key => {
//     if (key == "ossUrlList") {
//       params[key].forEach(url => {
//         formData.append("ossUrlList", url);
//       });
//     } else {
//       formData.append(key, params[key]);
//     }
//   });
//   return http.post<unknown, object>(
//     "/api/v1/material/video/upload",
//     { data: formData },
//     {
//       headers: {
//         "Content-Type": "multipart/form-data"
//       }
//     }
//   );
// };

// OSS 上传凭证响应接口
export interface OSSTokenResponse {
  accessid: string;
  host: string;
  expire: number;
  signature: string;
  policy: string;
  dir: string;
  callback: string;
}

// 获取 OSS 上传凭证
export const getOSSToken = () => {
  return http.request<OSSTokenResponse>(
    "get",
    "/api/v1/material/upload/oss/token"
  );
};
